import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/13 9:04
 * @description 笨办法
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] data = {240, 162, 138, 147, 145};
        System.out.println(solution.validUtf8(data));
    }
    public boolean validUtf8(int[] data) {
        int n = data.length, left;
        String[] S = new String[n];
        for (int i = 0; i < n; i++) {
            S[i] = Integer.toString(data[i], 2);
            if (S[i].length() < 8) {
                String temp = "";
                for (int j = 0; j < 8 - S[i].length(); j++) {
                    temp += "0";
                }
                S[i] = temp + S[i];
            }
        }
        for (int i = 0; i < n; i++) {
            if (S[i].startsWith("0")) {
                continue;
            }
            if (S[i].startsWith("110")) {
                if (i + 1 >= n) {
                    return false;
                }
                if (!S[++i].startsWith("10")) {
                    return false;
                }
            }else if (S[i].startsWith("1110")) {
                if (i + 2 >= n) {
                    return false;
                }
                for (left = i+1; left <= i+2; left++) {
                    if (!S[left].startsWith("10")) {
                        return false;
                    }
                }
                i = left - 1;
            }else if (S[i].startsWith("11110")) {
                if (i + 3 >= n) {
                    return false;
                }
                for (left = i+1; left <= i+3 ; left++) {
                    if (!S[left].startsWith("10")) {
                        return false;
                    }
                }
                i = left - 1;
            }else {
                return false;
            }
        }
        return true;
    }
}
